<template>
  <div>
    <c-table-box ref="cTableBox" hide-func checkbox :other-filters="otherFilters" :table-config="tableConfig" @selection-change="selectionChange">
      <template #btn>
        <c-permission v-if="type==='all'" code="all">
          <c-button opt-code="submitException" type="primary" @click="toAbnormal">发起异常</c-button>
          <Postpone opt-code="rectifydelay" :sel-data="selections" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='rectify'" code="rectify">
          <c-button type="primary" opt-code="submitrectify" @click="toSubmitInfo">整改资料提报</c-button>
        </c-permission>
      </template>
    </c-table-box>
  </div>
</template>
<script>
import { otherFiltersItem } from '@/utils'
import Postpone from './postpone'
export default {
  name: 'List',
  components: {
    Postpone
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    },
    activeType: {
      type: String,
      default: ''
    },
    rectifyType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tableConfig: {
        interfacePath: '/slm-admin/api/v1/acceptRectifyTask/selectPage',
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-perCode': this.rectifyType === 'SOFT' ? 'rectifysoft' : 'rectifyhard',
          'Permission-App': 'slm'
        },
        setting: []
      },
      allSetting: [
        {
          prop: 'taskCode',
          label: '整改任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200
          // className: 'btn-green',
          // isButton: true,
          // clickFun: this.toDetail
        },
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 200
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 120,
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'rectifyType',
          label: '整改类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'expectEndTime',
          label: '应整改完成时间',
          width: 140,
          formatData: this.$parseTimeYMD('expectEndTime')
        },
        {
          prop: 'isOutTime',
          label: '整改是否超时',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'schedule',
          label: '整改进度',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'ACCEPT_RECTIFY_TASK_SCHEDULE'
          }
        },
        {
          prop: 'steps',
          label: '整改步骤',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'RECTIFY_STEPS'
          }
        },
        {
          prop: 'belongDuty',
          width: 100,
          label: '责任归属',
          hidden: this.rectifyType !== 'SOFT'
        },
        {
          prop: 'shopName',
          width: 100,
          label: '所属门店'
        },
        {
          prop: 'officeName',
          width: 100,
          label: '办事处'
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'acceptPrincipalName',
          width: 100,
          label: '责任人'
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          width: 100
        },
        {
          prop: 'formSupervisorName',
          label: '形象督导',
          width: 100
        }
      ],
      rectifySetting: [
        {
          prop: 'taskCode',
          label: '整改任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200
          // className: 'btn-green',
          // isButton: true,
          // clickFun: this.toDetail
        },
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 120,
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'rectifyType',
          label: '整改类型',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'expectEndTime',
          label: '应整改完成时间',
          width: 140,
          formatData: this.$parseTimeYMD('expectEndTime')
        },
        {
          prop: 'schedule',
          label: '整改进度',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'ACCEPT_RECTIFY_TASK_SCHEDULE'
          }
        },
        {
          prop: 'steps',
          label: '整改步骤',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'RECTIFY_STEPS'
          }
        },
        {
          prop: 'belongDuty',
          width: 100,
          label: '责任归属',
          hidden: this.rectifyType !== 'SOFT'
        },
        {
          prop: 'shopName',
          label: '所属门店',
          width: 100
        },
        {
          prop: 'officeName',
          label: '办事处',
          width: 100
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 100,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          width: 100
        },
        {
          prop: 'formSupervisorName',
          label: '形象督导',
          width: 100
        }
      ],
      typeFilters: [],
      selections: []
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
        this.setTableSetting(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.refreshTableData()
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready && this.refreshTableData) {
      this.refreshTableData()
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    // 设置table setting
    setTableSetting(v) {
      switch (v) {
        case 'all':
          this.$set(this.tableConfig, 'setting', this.allSetting)
          break
        case 'rectify':
          this.$set(this.tableConfig, 'setting', this.rectifySetting)
          break
      }
    },
    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = [otherFiltersItem('rectifyType', this.rectifyType, '=')]
          break
        case 'rectify':
          this.typeFilters = [
            otherFiltersItem('rectifyType', this.rectifyType, '='),
            otherFiltersItem('steps', 'K1', '=')
          ]
          break
      }
    },
    refreshTableData(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },
    selectionChange(v) {
      this.selections = v
    },
    // 跳转
    clickToRouterHandle(name) {
      let msg = '整改资料提报'
      if (name === 'RectifyAbnormalSoft' || name === 'RectifyAbnormalHard') {
        msg = '发起异常'
      }
      if (!this.singleJudge(this.selections, msg, true)) {
        return
      }
      const s = this.selections[0]
      this.$router.push({ name, query: { id: s.id, taskCode: s.decorationTaskCode, unitCode: s.constUnitCode }})
    },
    toAbnormal() {
      this.clickToRouterHandle(this.rectifyType === 'SOFT' ? 'RectifyAbnormalSoft' : 'RectifyAbnormalHard')
    },
    toSubmitInfo() {
      this.clickToRouterHandle(this.rectifyType === 'SOFT' ? 'RectifySubmitInfoSoft' : 'RectifySubmitInfoHard')
    },
    // 详情
    toDetail(row) {
      this.$router.push({ name: this.rectifyType === 'SOFT' ? 'RectifyDetailSoft' : 'RectifyDetailHard', query: { id: row.id, taskCode: row.decorationTaskCode, unitCode: row.constUnitCode }})
    }
  }
}
</script>
